/*
*  Copyright 2019-2023 CY
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.ejweixin.rest;

import me.zhengjie.annotation.Log;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.modules.ejweixin.domain.NgUserLike;
import me.zhengjie.modules.ejweixin.service.NgUserLikeService;
import me.zhengjie.modules.ejweixin.domain.vo.NgUserLikeQueryCriteria;
import lombok.RequiredArgsConstructor;
import java.util.List;

import me.zhengjie.utils.SecurityUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import me.zhengjie.utils.PageResult;

/**
* @author cy
* @date 2025-04-29
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "用户喜欢管理")
@RequestMapping("/api/ngUserLike")
public class NgUserLikeController {

    private final NgUserLikeService ngUserLikeService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    public void exportNgUserLike(HttpServletResponse response, NgUserLikeQueryCriteria criteria) throws IOException {
        ngUserLikeService.download(ngUserLikeService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询用户喜欢")
    @ApiOperation("查询用户喜欢")
    public ResponseEntity<PageResult<NgUserLike>> queryNgUserLike(NgUserLikeQueryCriteria criteria, Page<Object> page){
        try {
            UserDetails loginUser = SecurityUtils.getCurrentUser();
            criteria.setUserName(loginUser.getUsername());
            return new ResponseEntity<>(ngUserLikeService.queryAll(criteria,page),HttpStatus.OK);
        }catch (BadRequestException e){
            System.out.println("未找到登陆的用户信息");
            return new ResponseEntity<>(HttpStatus.OK);
        }
    }

    @PostMapping
    @Log("新增用户喜欢")
    @ApiOperation("新增用户喜欢")
    public ResponseEntity<Object> createNgUserLike(@Validated @RequestBody NgUserLike resources){
        ngUserLikeService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改用户喜欢")
    @ApiOperation("修改用户喜欢")
    public ResponseEntity<Object> updateNgUserLike(@Validated @RequestBody NgUserLike resources){
        ngUserLikeService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除用户喜欢")
    @ApiOperation("删除用户喜欢")
    public ResponseEntity<Object> deleteNgUserLike(@RequestBody List<Integer> ids) {
        try {
            UserDetails loginUser = SecurityUtils.getCurrentUser();
            ngUserLikeService.deleteAll(ids, loginUser.getUsername());
            return new ResponseEntity<>(HttpStatus.OK);
        }catch (BadRequestException e){
            System.out.println("未找到登陆的用户信息");
            return new ResponseEntity<>(HttpStatus.OK);
        }
    }

    @DeleteMapping("deleteNgUserLikeByType")
    @Log("删除用户喜欢")
    @ApiOperation("删除用户喜欢")
    public ResponseEntity<Object> deleteNgUserLikeByType(@Validated @RequestBody NgUserLike resources) {
        try {
            Long userId = SecurityUtils.getCurrentUserId();
            ngUserLikeService.deleteByUserIdAndDataIdAndTagType(resources.getTagType(), userId, resources.getDataId());
            return new ResponseEntity<>(HttpStatus.OK);
        }catch (BadRequestException e){
            System.out.println("未找到登陆的用户信息");
            return new ResponseEntity<>(HttpStatus.OK);
        }
    }
}